//
//  CYStatusCell.swift
//  WeiBo
//
//  Created by Mr_J on 16/3/9.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

class CYStatusCell: UITableViewCell {
    
    var toolBarTopConstraint: Constraint?
    
    var statusViewModel: CYStatusViewModel? {
        didSet {
            originalStatusView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            
            self.toolBarTopConstraint?.uninstall()
            
            
            if statusViewModel?.status?.retweeted_status != nil {
//                print("有转发微博")
                retweetStatusView.statusViewModel = statusViewModel
                
                retweetStatusView.hidden = false
                
                
                statusToolBar.snp_makeConstraints(closure: { (make) -> Void in
                   self.toolBarTopConstraint =  make.top.equalTo(retweetStatusView.snp_bottom).constraint
                })
            }else {
//                print("没有转发微博")
                
                retweetStatusView.hidden = true
                
                statusToolBar.snp_makeConstraints(closure: { (make) -> Void in
                   self.toolBarTopConstraint = make.top.equalTo(originalStatusView.snp_bottom).constraint
                })
                
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        selectionStyle = .None
        
        contentView.backgroundColor = UIColor(white: 239/255, alpha: 1)
        
        contentView.addSubview(originalStatusView)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(retweetStatusView)
        
        originalStatusView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(CYStatusCellMargin)
//            make.height.equalTo(50)
//            (200)
            
        }
        statusToolBar.snp_makeConstraints { (make) -> Void in
            make.trailing.leading.equalTo(originalStatusView)
//            make.top.equalTo(originalStatusView.snp_bottom)
           self.toolBarTopConstraint = make.top.equalTo(retweetStatusView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(statusToolBar)
        }
        
        retweetStatusView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalStatusView.snp_bottom)
            make.leading.trailing.equalTo(originalStatusView)
        }
        
    }
    
    
    
    private lazy var originalStatusView: CYOriginalStatusView = CYOriginalStatusView()
    
    
    private lazy var statusToolBar: CYStatusToolBar = {
        let toolBar = CYStatusToolBar()
        
        
        return toolBar
    }()
    
    private lazy var retweetStatusView: CYRetweetStatusView = CYRetweetStatusView()
}
